import base64
import zlib
import json
import time

# #  二进制解压 iP  网页复制 token对应的值DATA
# data = 'eJxFjktvqzAQhf+Lt6Bgg3k4UhfNowpOKKUBAlRdUELCIyRxMAR6df97XalVpRmdM2e+xfkHbvYeTBGEBEIZ9PkNTAGawIkBZMBb8TF0qGNs6gbULRlkf5lYZGlEBh+3cAGmb8iAWEY6fP9OXkXwhogKZQQtEf16TX2XVSzmm7IFBArOr+1UUT6qSZOXvEvPk+zSKMK3RamIFkCgjS9QofWPpj/Kf29H1BZsWx7PwuX0fqoC1N3HR6/Ipe0saK3BxsMpLIJduGNt187WFDf1wO6xXQau6h+2krd0A2JWiCLiY9In0vMoFXly7OeulSj2BufRcmFmSm8c+OeTv3gqNZalUlLrHA/2GrmXJCENf7wux/O2lmgc5SrNR/QS9Qk5ee6GXXrvure1TemzoWuc27NzzijT0qqh+9VKqleU7Vk0Svi4i4YyPHFm75QX7serOK1mhsNo2Lha5bmf5GpmiQRnThybQ4DVedN3pFjrwXKe8MP28ko33sMD+P8F7eaQDw=='

#  递归的写法
# def Token123(data):
#     token_encode = base64.b64decode(data.encode())
#     # 二进制解压
#     token_sting = zlib.decompress(token_encode).decode('utf-8')
#     print(token_sting) #  iP
#
#     sign = json.loads(token_sting)['sign']
#     Token123(sign) # 请求参数

import requests


def decode_sign(token_string):
    # base编码
    encode1 = str(token_string).encode()
    # 参数 压缩成 特殊的编码 ->
    compress = zlib.compress(encode1)  # 二进制压缩
    b_encode = base64.b64encode(compress)
    # 转变 str
    e_sign = str(b_encode, encoding='utf-8')
    return e_sign


# web页面的
params = {
    'cityName': '北京',
    'cateId': 0,
    'areaId': 0,
    'sort': '',
    'dinnerCountAttrId': '',
    'page': 1,
    'userId': '1095709749',
    'uuid': '4c075c877a634780999c.1649833302.1.0.0',
    'platform': 1,
    'partner': 126,
    'originUrl': 'https://bj.meituan.com/meishi/',
    'riskLevel': 1,
    'optimusCode': 10
}
# 生成sign
sign = decode_sign(params)
iP = {
    "rId": 100900,
    "ver": "1.0.6",
    "ts": int(round(time.time() * 1000)),
    "cts": int(round(time.time() * 1000) + 2000),
    "brVD": [
        1604,
        150
    ],
    "brR": [
        [
            1920,
            1080
        ],
        [
            1920,
            1032
        ],
        24,
        24
    ],
    "bI": [
        "https://bj.meituan.com/meishi/",
        ""
    ],
    "mT": [
        "347,565",
        "340,546",
        "331,523",
        "323,498",
        "312,475",
        "300,451",
        "288,433",
        "288,433",
        "490,513",
        "468,545",
        "468,545"
    ],
    "kT": [],
    "aT": [],
    "tT": [],
    "aM": "",
    "sign":sign
}
# 生成token
params['_token'] = decode_sign(iP)

url = 'https://bj.meituan.com/meishi/api/poi/getPoiList?'
headers = {
    # 用户信息
    'Cookie': 'uuid=4c075c877a634780999c.1649833302.1.0.0; _lxsdk_cuid=18021ba6b1cc8-04e07cf948d615-1734337f-1fa400-18021ba6b1cc8; _lxsdk=18021ba6b1cc8-04e07cf948d615-1734337f-1fa400-18021ba6b1cc8; _lx_utm=utm_source%3Dbing%26utm_medium%3Dorganic; ci=1; rvct=1; __mta=213325739.1650540352244.1650540352244.1650540352244.1; client-id=95759446-8ab4-46dc-941f-e980593a83b0; mtcdn=K; userTicket=lRzjeeVdCRwOSAoOdMTYopViqbRwfHwqRRXcRbqN; u=1095709749; n=Gqh370074984; lt=9vjFI1mJA47TntxyzuAFlolQa7EAAAAAfhEAAOLi6s1DEqrdK4wAyJME7fmrC-aL9k7tXcl6DX8BP79gtswTAK5obOft9ar0xxNsVQ; mt_c_token=9vjFI1mJA47TntxyzuAFlolQa7EAAAAAfhEAAOLi6s1DEqrdK4wAyJME7fmrC-aL9k7tXcl6DX8BP79gtswTAK5obOft9ar0xxNsVQ; token=9vjFI1mJA47TntxyzuAFlolQa7EAAAAAfhEAAOLi6s1DEqrdK4wAyJME7fmrC-aL9k7tXcl6DX8BP79gtswTAK5obOft9ar0xxNsVQ; token2=9vjFI1mJA47TntxyzuAFlolQa7EAAAAAfhEAAOLi6s1DEqrdK4wAyJME7fmrC-aL9k7tXcl6DX8BP79gtswTAK5obOft9ar0xxNsVQ; unc=Gqh370074984; firstTime=1650544752179; _lxsdk_s=1804c0d3611-a17-d78-0db%7C%7C8',
    'Host': 'bj.meituan.com',
    'Referer': 'https://bj.meituan.com/meishi/',
    # 浏览器类型
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36'
}
response = requests.get(url, headers=headers, params=params).json()
print(response)



# h5 页面的
# url = 'https://i.waimai.meituan.com/openh5/homepage/poilist?_=1649593929487&X-FOR-WITH=rRvBpda%2B3dlqS5OTiSwvjBe7o%2BTDxfL45PXK1%2F4pCp%2FoW7SjVmm1XQEe%2FyZ6qQCEN4T%2FqCWFLj3N7Q2nG59L5Rpe1ni4SA2u7C%2FfQ7YhIpAE3yBqCq7xa%2Bbnqai%2BS%2BhdswLNctJt2zrvat7PJKuJeUT8GYUsy8Horn9GwOVBd4dMBQQd7OFANOGDI2QfRc7Wapt5v6clnFbfp0qM%2Bkgv6Q%3D%3D'
# data = {
#     'startIndex': 0,
#     'sortId': 0,
#     'multiFilterIds': '',
#     'sliderSelectCode': '',
#     'sliderSelectMin': '',
#     'sliderSelectMax': '',
#     'geoType': 2,
#     'rankTraceId': '',
#     'uuid': '5E1E6CA73986FA76CD98B7D1AA0DEAFED5DD903A488A6908959CA03931A15817',
#     'platform': 3,
#     'partner': 4,
#     'originUrl': 'https://h5.waimai.meituan.com/waimai/mindex/home',
#     'riskLevel': 71,
#     'optimusCode': 10,
#     'firstcategoryid': 0,
#     'wm_latitude': '28199359',
#     'wm_longitude': '112890849',
#     'wm_actual_latitude': '28199359',
#     'wm_actual_longitude': '112890849',
#     'wmUuidDeregistration': 0,
#     'wmUserIdDeregistration': 0,
#     'openh5_uuid': '5E1E6CA73986FA76CD98B7D1AA0DEAFED5DD903A488A6908959CA03931A15817',
#     # '_token': token
# }
# sign = decode_sign(data)
#
# Ip = {
#     "rId": 101701,
#     "ver": "1.0.6",
#     "ts": 1649589480416,
#     "cts": 1649589480734,
#     "brVD": [1530, 202],
#     "brR": [[1920, 1080], [1920, 1036], 24, 24],
#     "bI": ["https://h5.waimai.meituan.com/waimai/mindex/home", ""],
#     "mT": [],
#     "kT": [],
#     "aT": [],
#     "tT": [],
#     "aM": "",
#     "sign": sign
# }
# token = decode_sign(Ip)
# data['_token'] = token
# headers = {
#     'Cookie': '_lxsdk_cuid=17f739c3cd3c8-017bc73394a958-977173c-1fa400-17f739c3cd3c8; _hc.v=0876198d-b5c4-2526-1196-1daf80434e12.1646913994; ci=1; rvct=1; iuuid=5E1E6CA73986FA76CD98B7D1AA0DEAFED5DD903A488A6908959CA03931A15817; cityname=%E5%8C%97%E4%BA%AC; _lxsdk=5E1E6CA73986FA76CD98B7D1AA0DEAFED5DD903A488A6908959CA03931A15817; wm_order_channel=default; request_source=openh5; terminal=i; w_utmz="utm_campaign=(direct)&utm_source=5000&utm_medium=(none)&utm_content=(none)&utm_term=(none)"; openh5_uuid=5E1E6CA73986FA76CD98B7D1AA0DEAFED5DD903A488A6908959CA03931A15817; w_latlng=28199359,112890849; w_actual_lat=28199359; w_actual_lng=112890849; mtcdn=K; isid=a9RSPwUk-2Jgm30XDHjXCtJ0pAsAAAAAQREAAGRhuyQbu5CvUWjCeOUJv-pvvIPEpCLVK7oFpkAhvYlpcA_gDIsKUvS-5QdV0CVVvg; mt_c_token=a9RSPwUk-2Jgm30XDHjXCtJ0pAsAAAAAQREAAGRhuyQbu5CvUWjCeOUJv-pvvIPEpCLVK7oFpkAhvYlpcA_gDIsKUvS-5QdV0CVVvg; logintype=normal; au_trace_key_net=default; openh5_uuid=5E1E6CA73986FA76CD98B7D1AA0DEAFED5DD903A488A6908959CA03931A15817; uuid=5E1E6CA73986FA76CD98B7D1AA0DEAFED5DD903A488A6908959CA03931A15817; _lx_utm=utm_source%3D; w_visitid=719e9d3a-3112-405d-9d2d-b3fab29528ae; _lxsdk_s=18013653cb6-ad8-6c9-85b%7C%7C90; cssVersion=b53af8a9',
#     'Host': 'i.waimai.meituan.com',
#     'Origin': 'https://h5.waimai.meituan.com',
#     'Referer': 'https://h5.waimai.meituan.com/',
#     'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36'
# }
# response = requests.post(url, headers=headers, data=data).text
# print(response)
